package com.tools.entity;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * http请求头对象
 */
public class HttpParam {
    private Map<String,Object> params = new HashMap<>();

    public final HttpParam addParam(String name,String value){
        if(StringUtils.isEmpty(name)){
            return this;
        }
        params.put(name,value);
        return this;
    }
    public final List<NameValuePair> toPostHttpParam(){
        List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size());
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            pairList.add(new BasicNameValuePair(entry.getKey(),	entry.getValue().toString()));
        }
        return pairList;
    }

    public final String toGetHttpParam(){
        StringBuffer param = new StringBuffer();
        int i = 0;
        for (String key : params.keySet()) {
            if (i == 0) {
                param.append("?");
            }else {
                param.append("&");
            }
            param.append(key).append("=").append(params.get(key));
            i++;
        }
        return param.toString();
    }
}
